using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.Validation.Tests
{
    [TestClass]
    public class ExternallyConfiguredObjectValidatorFixture
    {
        [TestMethod, DeploymentItem("FooValidation.config")]
        public void ReferencedObjectCanBeValidatedFromExternalConfigUsingAttribute()
        {
            TestTargetWithAttribute target = new TestTargetWithAttribute();
            target.ValidateMeUsingFooRules1.Value = "value"; //validation using "FooRules1" succeeds
            target.ValidateMeUsingFooRules2.Value = null; //validation using "FooRules2" succeeds

            Validator validator = ValidationFactory.CreateValidatorFromAttributes<TestTargetWithAttribute>();
            Assert.IsTrue(validator.Validate(target).IsValid);


            target.ValidateMeUsingFooRules2.Value = "value"; //validation using "FooRules2" fails
            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(1, new List<ValidationResult>(validator.Validate(target)).Count);

            target.ValidateMeUsingFooRules2.Value = null; //validation using "FooRules2" succeeds
            target.ValidateMeUsingFooRules1.Value = null; //validation using "FooRules1" fails

            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(1, new List<ValidationResult>(validator.Validate(target)).Count);


            target.ValidateMeUsingFooRules2.Value = "value"; //validation using "FooRules2" fails
            target.ValidateMeUsingFooRules1.Value = null; //validation using "FooRules1" fails

            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(2, new List<ValidationResult>(validator.Validate(target)).Count);

        }

        [TestMethod, DeploymentItem("FooValidation.config"), DeploymentItem("TestTargetVAlidation.config")]
        public void ReferencedObjectCanBeValidatedFromExternalConfigUsingConfig()
        {
            TestTargetWithConfig target = new TestTargetWithConfig();
            target.ValidateMeUsingFooRules1.Value = "value"; //validation using "FooRules1" succeeds
            target.ValidateMeUsingFooRules2.Value = null; //validation using "FooRules2" succeeds

            Validator validator = ValidationFactory.CreateValidatorFromConfiguration(typeof(TestTargetWithConfig), string.Empty, new FileConfigurationSource("TestTargetValidation.config"));
            Assert.IsTrue(validator.Validate(target).IsValid);


            target.ValidateMeUsingFooRules2.Value = "value"; //validation using "FooRules2" fails
            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(1, new List<ValidationResult>(validator.Validate(target)).Count);

            target.ValidateMeUsingFooRules2.Value = null; //validation using "FooRules2" succeeds
            target.ValidateMeUsingFooRules1.Value = null; //validation using "FooRules1" fails

            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(1, new List<ValidationResult>(validator.Validate(target)).Count);


            target.ValidateMeUsingFooRules2.Value = "value"; //validation using "FooRules2" fails
            target.ValidateMeUsingFooRules1.Value = null; //validation using "FooRules1" fails

            Assert.IsFalse(validator.Validate(target).IsValid);
            Assert.AreEqual(2, new List<ValidationResult>(validator.Validate(target)).Count);

        }
    }
}
